/**
 * 扑克类游戏头像管理
 */
var ClientDelegate = require('./../../FrameWork/Delegate/ClientDelegate');
var ClientEveDelegate = require('./../../FrameWork/Delegate/ClientEveDelegate');
var ClientPkDelegateType = require('./../Delegate/ClientPkDelegateType');

var _ = require('./../../FrameWork/we_underscore');
var UtilGameObject = require('./../../FrameWork/Utils/UtilGameObject');
module.exports = cc.Class({
    extends: ClientDelegate,
    properties: {
        posHeadList: [cc.Node],
        prefabHead: {
            default: null,
            type: cc.Prefab,
            displayName: '头像预设'
        },
        prefabIcon: {
            default: null,
            type: cc.Prefab,
            displayName: '图预设'
        },
        prefabCardLeft: {
            default: null,
            type: cc.Prefab,
            displayName: '剩余牌数量预设'
        },
        prefabLbTimeOut: {
            default: null,
            type: cc.Prefab,
            displayName: '倒计时文本对象'
        },
    },
    onLoad() {
        this._super();
        this.addListerDelegate(ClientPkDelegateType.Server_Sure_Landlord, this.doLandlordTip.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_showHeadDoubleUI, this.doShowDoubleUI.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_cleanAllHeadUI, this.doCleanAllHeadUI.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_refreshCardLeftCnt, this.doRefreshCardLeftCnt.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_showOtherTimeOut, this.doTimeOut.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_cleanAllTimeOutUI, this.doCleanAllTimeOut.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_cleanSeatIdxTimeOutUI, this.doCleanTimeOutSeatIdx.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_hideLeftCardCntUI, this.hideLeftCardCntUI.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Client_refreshHeadMoney, this.refreshMoneyUI.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Client_headAddMoney, this.addMoneyAnim.bind(this));


        this.offsetX = 5; //偏移头像的距离
        this.headCtrList = [];
        this.uiCtrList = [];
        this.timeOutCtrList = {};
        this.cardBgCtrList = {};
        _.each(this.posHeadList, function(_pos) {
            var ctr = UtilGameObject.getCtrByPrefabs(this.prefabHead, _pos);
            ctr.setActive(false);
            this.headCtrList.push(ctr);
            _pos.removeComponent(cc.Widget);
            if (window.g_room) {
                let seatObj = window.g_room.getSeatByIdx(_pos);
                if (!!seatObj) {
                    let money = seatObj.getMoney();
                    ctr.refreshMoney(money);
                }
            }

        }.bind(this));
        this.iniTest();
        this.resetNet();
    },
    resetNet() {
        // this.headCtrList = [];
        // this.uiCtrList = [];
        if (window.g_room) {
            var selfSeatIdx = window.g_room.getSelfSeatIdx();
            window.g_room.each(function(seatObj) {
                var seatIdx = seatObj.getSeatIdx();
                if (selfSeatIdx != seatIdx) {
                    this.doRefreshCardLeftCnt(seatObj.getSeatInfo());
                }
            }.bind(this));
        }
    },
    showHeadAll() {
        _.each(this.headCtrList, function(ctr, _seatIdx) {
            ctr.clean();
            ctr.setActive(true);
            ctr.refreshMoney(0);
            if (window.g_room) {
                let seatObj = window.g_room.getSeatByIdx(_seatIdx);
                if (!!seatObj) {
                    let money = seatObj.getMoney();
                    ctr.refreshMoney(money);
                }
            }
        });
    },
    /***jData:{seatIdx:1,money:1} */
    refreshMoneyUI(jData) {
        let seatIdx = jData.seatIdx;
        let money = jData.money;
        if (this.headCtrList[seatIdx]) {
            this.headCtrList[seatIdx].refreshMoney(money);
        }
    },
    /***jData:{seatIdx:1,money:1} */
    addMoneyAnim(jData) {
        let seatIdx = jData.seatIdx;
        let money = jData.money;
        if (this.headCtrList[seatIdx]) {
            this.headCtrList[seatIdx].addMoney(money);
        }
    },

    /***地主标识*/
    doLandlordTip(_landlordSeatIdx) {
        var pos = this.posHeadList[_landlordSeatIdx];
        var ctr = UtilGameObject.getCtrByPrefabs(this.prefabIcon, pos);
        var dre = this.getDrection(_landlordSeatIdx);
        ctr.showLandlordTag();

        var headSize = pos.getContentSize();
        var iconSize = ctr.getContentSize();

        var x = (headSize.width + iconSize.width) * 0.5 + this.offsetX;
        ctr.setPositionX(x * dre);
        var y = 0 == _landlordSeatIdx ? ((headSize.height - iconSize.height * 1.11) * 0.5) : -((headSize.height - iconSize.height * 0.8) * 0.5)
        ctr.setPositionY(y);
        this.uiCtrList.push(ctr);
    },

    /**
     * 头像上加倍UI
     * @param {seatIdx:1//座位号,double:1//加倍值} _info 
     */
    doShowDoubleUI(_info) {
        var seatIdx = _info.seatIdx;
        var double = _info.double;
        this.doCleanTimeOutSeatIdx(seatIdx);
        if (1 == double) {
            var pos = this.posHeadList[seatIdx];
            var ctr = UtilGameObject.getCtrByPrefabs(this.prefabIcon, pos);
            ctr.showDoubleIcon();
            var headSize = pos.getContentSize();
            var iconSize = ctr.getContentSize();
            var x = (-headSize.width + iconSize.width) * 0.5;
            var y = (headSize.height - iconSize.height * 1.3) * 0.5;
            ctr.setPositionX(x);
            ctr.setPositionY(y);
            this.uiCtrList.push(ctr);
        }
    },
    doCleanAllHeadUI() {
        _.each(this.uiCtrList, function(ctr) {
            if (!!ctr && !!ctr.node) {
                ctr.node.destroy();
            }
        });
        this.uiCtrList
    },

    hideLeftCardCntUI() {
        _.each(this.cardBgCtrList, function(_ctr) {
            _ctr.setActive(false);
        })
    },

    /***刷新牌剩余数量 
     * _info:{seatIdx:1,cardLeftCnt:1}
     */
    doRefreshCardLeftCnt(_info) {
        var seatIdx = _info.seatIdx;
        var cardLeftCnt = _info.cardLeftCnt;
        var pos = this.posHeadList[seatIdx];
        if (null == this.cardBgCtrList[seatIdx]) {
            var ctr = UtilGameObject.getCtrByPrefabs(this.prefabCardLeft, pos);
            var dre = this.getDrection(seatIdx);
            var headSize = pos.getContentSize();
            var iconSize = ctr.getContentSize();
            var x = (headSize.width + iconSize.width) * 0.5 + this.offsetX;
            var y = (headSize.height - iconSize.height * 1.11) * 0.5;
            ctr.setPositionX(x * dre);
            ctr.setPositionY(y);
            this.cardBgCtrList[seatIdx] = ctr;
        }
        this.cardBgCtrList[seatIdx].setActive(true);
        this.cardBgCtrList[seatIdx].refreshCardLeft(cardLeftCnt);
    },

    doCleanAllTimeOut() {
        _.each(this.timeOutCtrList, function(ctr) {
            if (!!ctr && !!ctr.node) {
                ctr.node.destroy();
            }
        });
        this.timeOutCtrList = {};
    },

    doCleanTimeOutSeatIdx(_seatIdx) {
        if (!!this.timeOutCtrList && !!this.timeOutCtrList[_seatIdx]) {
            this.timeOutCtrList[_seatIdx].node.destroy();
            this.timeOutCtrList[_seatIdx] = null;
        }
    },

    /***倒计时
     * _info:{seatIdx:1}
     */
    doTimeOut(_info) {
        var seatIdx = _info.seatIdx;
        this.doCleanTimeOutSeatIdx(seatIdx);
        var pos = this.posHeadList[seatIdx];
        var ctr = UtilGameObject.getCtrByPrefabs(this.prefabLbTimeOut, pos, "TimeOutCtr");
        ctr.startCountDown(30, function() {}.bind(this));
        var headSize = pos.getContentSize();
        var iconSize = ctr.getContentSize();
        var dre = this.getDrection(seatIdx);
        var x = (headSize.width + iconSize.width) + this.offsetX;
        ctr.setPositionX(x * dre);
        this.timeOutCtrList[seatIdx] = ctr;
    },
    iniTest() {
        // UtilGameObject.createDemoBtn([
        //     ['头像', function() {
        //             this.showHeadAll();
        //         }.bind(this)

        //     ],
        // ]);
        this.showHeadAll();
    },

    getDrection(_seatIdx) {
        if (0 == _seatIdx || 2 == _seatIdx) {
            return 1;
        }
        return -1;
    }
});